<?php

/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * http://www.gnu.org/copyleft/gpl.html
 */
 


/**
 *  @file
 *  @Author Samuel Maura
 *  Create a wiki entry
 */


session_start();
require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/controllers/authorize.php");
require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/models/restrictions.php");
require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/models/groups.php");
require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/views/html.php");
require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/lib/session/sessionUtils.inc");
require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/models/entries.php");

$html = new Html();
$html->pageHeaderStart('Wiki:Create');
$html->mainStyle();
$html->style('registration');
$html->pageHeaderEnd();
$html->pageBodyStart();

if (isset($_SESSION['AUTHORIZED']) && ($_SESSION['AUTHORIZED'] == 1) &&  ($_SESSION['username'] != '') ){
  $html->displayLoginMenu($_SESSION['username']);
   $html->displayWikiMenu($_SESSION['username']);
  
}
 
 if (isset($_GET['action']) && isset($_GET['page_id']) && ($_GET['action'] != '')  && ($_GET['page_id'] != '') ){
   $entries = new Entries();
   $entry_array = $entries->getPage($_GET['page_id']);
   if ( ($entry_array !== 0) && is_array($entry_array) ){
     foreach( $entry_array as $key => $value){
      $page_title   = $entries->displayTitle($entry_array[$key]['title']);
      $page_content =  $entry_array[$key]['content'];
     
     }
   }
   $html->displayLabel('Action');  
 }
 else{
   $html->displayLabel('New Page');
 }



// Error block
if ( isset($_SESSION['validation_error']) && ($_SESSION['validation_error'] != '')){
  $html->displayErrorBlock($_SESSION['validation_error']);
}

?>

<div class='hspacer'></div>

<div>
 <form method="post" action="../controllers/createpage.php">
  <div>
    <div><label for="title">title:</label></div>
     <div><input type="text" id="title" size='70' name="title" 
     value="<? if (isset($page_title) ){echo $page_title;} else {sessionPrint('title'); } ?>" 
     <? if (isset($_GET['action']) && ($_GET['action'] == 'edit') ){echo 'readonly=readonly';}?> 
     /> </div>
  </div>
  <div class='hspacer'></div>
  <div>
    <div><label for="content">Content:</label></div>
     <div>
       <textarea id="content" name="content" rows='10' cols='60'>
       <? if (isset($page_content) ){echo $page_content;} else {sessionPrint('content'); } ?>
       </textarea> 
     </div>
  </div>
  
  <div class='hspacer'></div> 
    <div><label for="access"> Access rules:</label></div>
     <div>
       <select id="access" name="access">
         <?
           $restrictions = new SystemRestrictions();
           $result_array = $restrictions->getAllRestrictions();
           if (is_array($result_array)){
             foreach ($result_array as $key => $val){
               echo "<option value='".$val['id']."' ";
               echo ( $val['label'] == 'author') ? 'selected' : '';
               echo ">".$val['label']."</option>";
             }
           }
         ?>
       </select> 
   </div>
   
   <div class='hspacer'></div> 
    <div><label for="group"> Select Group:</label></div>
     <div>
       <select id="group" name="group">
           <?
           $groups = new Groups();
           $result_array = $groups->getAllGroups();
           if (is_array($result_array)){
             foreach ($result_array as $key => $val){
               echo "<option value='".$val['id']."' ";
               echo ">".$val['label']."</option>";
             }
           }
         ?>
       </select> 
   </div>
   
   <div id="add_group"><a href="return false;" 
   onclick="window.open('<?echo "http://".$_SERVER['SERVER_ADDR']."/xenix/views/wikicreategroup.php"?>','mywindow',
   'scrollbars=1,width=820,height=400');return false; ">Create new group</a></div>
  
  <div class='hspacer'></div>
  <?
   if ( isset($_GET['action']) && ($_GET['action'] == 'edit') ){
      echo "<input type='hidden' value='edit' name='action'>";
      echo "<input type='hidden' value='".$_GET['page_id']."' name='page_id'>";
      echo "<input type='hidden' value='".$_GET['version']."' name='revision'>";
      echo "<div><input type='submit' id='submit' value='Update' /></div>";
   }
   else{
   echo "<div><input type='submit' id='submit' value='Create' /></div>";
  }
  ?>
</form>

</div>
<div class='hspacer'></div>
<?
$html->pageFooter();
$html->pageBodyEnd();
?>

<?
if ( isset($_SESSION['validation_error'])){
  $_SESSION['validation_error'] = '';   
}
?>
